package com.edar.service.platform.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edar.components.AccountBean;
import com.edar.dao.AccountDao;
import com.edar.dao.util.Page;
import com.edar.dto.AccountDTO;
import com.edar.model.Account;
import com.edar.service.impl.GenericServiceImpl;
import com.edar.service.platform.AccountService;
@Service
@Transactional
public class AccountServiceImpl extends GenericServiceImpl implements AccountService {
	/**
	 * 注入dao层
	 */
	@Autowired
	@Qualifier("accountImpl")
	private AccountDao accountDao;
	@Autowired
	@Qualifier("accountDto")
	private AccountDTO accountDto;
	
	@Transactional
	public void saveAccount(final Account account){
		accountDao.makePersistent(account);
	}
	@Transactional(readOnly=true)
	public Account getAccount(final Long accountId){
		Account account = accountDao.findById(accountId);
		accountDao.initialize(account);
		accountDto.trans(account);
		return account;
	}
	@Transactional
	public void delete(final Account account) {
		accountDao.makeTransient(account);
	}
	@Transactional
	public void save(final Account account) {
		accountDao.makePersistent(account);		
	}
	
	@Transactional(readOnly=true)
	public Page<AccountBean> getPage(final AccountBean accountBean){
		page =  new Page<AccountBean>();
		page.setTotal(accountDao.count(accountBean));
		page.setResult(accountDto.trans(accountDao.find(accountDao.assembly(accountBean))));
		return page;
	}

	public Page<AccountBean> getPage() {
		return page;
	}
	public void setPage(final Page<AccountBean> page) {
		this.page = page;
	}

	private Page<AccountBean> page;

	public void list(Page<AccountBean> page, AccountBean accountBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(accountDao.count(accountBean));
		}
		page.setResult(accountDto.trans(accountDao.findByPage(page,accountBean)));
	}
	/**
	 * 根据主键id列删除，批量删除用户
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			accountDao.makeTransient(accountDao.findById(Long.valueOf(id)));
			i++;
			if(i==MAX_CLEAR)
				accountDao.clear();
		}
		
	}
}
